package spzx.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.spzx.model.entity.product.Category;
import com.atguigu.spzx.model.vo.product.CategoryExcelVo;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletResponse;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import spzx.listener.CategoryReadListener;
import spzx.mapper.CategoryMapper;
import spzx.service.CategoryService;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<Category> findByParentId(Long parentId) {
        List<Category> list = categoryMapper.findByParentId(parentId);

        for (Category category : list) {
            /*// 查询当前分类下级分类的行数
            int size = categoryMapper.findByParentId(category.getId()).size();*/
            //查看三级目录的行数
            Integer size = categoryMapper.countByParentId(category.getId());

            // 行数>0,说明有下级分类
            category.setHasChildren(size > 0);
        }
        return list;
    }

    @Override
    public List<CategoryExcelVo> findAllCategoryVoList() {
        return categoryMapper.findAllCategoryVoList();
    }

    //调用此方法时，方法体一定是会执行的，并且在执行方法体之前，将该命名空间下的所有key删除
    @CacheEvict(value="category",beforeInvocation = true,allEntries = true)
    @Override
    public void upload(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),CategoryExcelVo.class,new CategoryReadListener(categoryMapper)).sheet().doRead();
            //EasyExcel.read(file.getInputStream(),CategoryExcelVo.class,new CategoryReadListener(categoryMapper，stringRedisTemplate)).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void download(HttpServletResponse response) {
        try {
            //1.设置响应头  文档类型（excel文件）+字符集+指定下载到客户端的文件名
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            //URLEncoder.encode()方法对文件名进行编码，防止中文乱码
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode("所有商品分类", "utf-8").replaceAll("\\+","%20") + ".xlsx");

            //2.从数据库中查询所有的商品分类
            List<CategoryExcelVo> excelVoList = this.findAllCategoryVoList();
            //3.使用EasyExcel工具类，将数据导出到excel文件中
            EasyExcel.write(response.getOutputStream(), CategoryExcelVo.class).sheet("全部商品分类").doWrite(excelVoList);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    public List<Category> findCategoryTree(Long pid) {
        List<Category> list = categoryMapper.findByParentId(pid);

        for (Category category : list) {
            category.setChildren(this.findCategoryTree(category.getId()));
        }

        return list;
    }

}
